package com.easypay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easypay.bean.UnionUserId;
import com.easypay.bean.cashier.Notice;
import com.easypay.bean.cashier.TradeQuery;
import com.easypay.bean.cashier.H5AdvancePay;
import com.easypay.bean.cashier.SAPPAdvancePay;
import com.easypay.util.HttpConnectUtils;
import com.easypay.util.RandomUtil;
import com.easypay.util.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/***
 *  云易收产品， 易生提供的聚合收银台
 *  接口地址：https://doc.eycard.cn/web/#/222?page_id=4003
 *
 * @date: 2021/10/22 10:54
 * @author: pandans
 */
public class CloudCashier extends BasePay{

    public static void main(String[] args) {

//        Map map = new HashMap<>();
//        map.put("name", "zs");
//        map.put("age", 20);
//
//        String sign = sign(map);
//        System.out.println(sign);

        // h5预下单接口。 通常webview， 公众号使用
//        doH5AdvancePay();
        // 小程序 预下单接口
        doSPPAdvancePay();

        // 查询交易
//        doTradeQuery();

        // 分期查询
//        doQuStaging();

        // 分期交易退货
//        doStandardResound();
    }


    /**
     * 分期查询
     */
    /*{
        "bizData": {
            "oriBizDate": "20211207",
            "oriOrgTrace": "1638865902332111638865902332"
        },
        "orgId": "ORG000000000002",
        "orgInfo": "123214214",
        "orgMerCode": "c",
        "orgTermNo": "W1486497",
        "orgTrace": "1638865902332111638865902332",
        "prodCode": "W03",
        "sign": "124213213",
        "signType": "RSA",
        "sysRetCode": ""
    }*/
    public static void doQuStaging() {

        Map<String, Object> request = new HashMap<>();
        request.put("orgId",ORGID);
        request.put("orgMerCode",ORGMERCODE);
        request.put("orgTermNo",ORGTERMNO);
        request.put("orgTrace",ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        request.put("prodCode","ISYH");

        Map<String, Object> bizData = new HashMap<>();
        bizData.put("oriBizDate", "20211230");//原交易日期
        bizData.put("oriOrgTrace", "222222222226");//原机构交易流水号
        request.put("bizData", bizData);
        String sign = sign(bizData, TEST_MER_RSA_PRIVATE_KEY);
        request.put("sign",sign);
        request.put("signType",SIGN_TYPE_RSA256);


        // 云闪付获取授权码  ！！是获取opneid的前置条件
//        doYsfQrAuth();

        String requestStr = JSON.toJSONString(request);
        try {
            String responce = HttpConnectUtils.sendHttpSRequest(CASHIER_URL + "/cashier/trade/queryStaging", requestStr, "JSON", null);
            System.out.println("responce ===>"+responce);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 分期交易退货
     */

    /*{
        "bizData": {
            "oriBizDate": "20210412",
            "oriOrgTrace": "222222222226",
            "transAmt": "1"
        },
        "orgId": "ORG000000000001",
        "orgInfo": "123214214",
        "orgMerCode": "531000012965833",
        "orgSmercode": "123214213213",
        "orgTermNo": "W1486540",
        "orgTrace": "222222222376",
        "prodCode": "2132142132",
        "prodTrace": "124213123123213",
        "sign": "124213213",
        "signType": "214213213"
    }*/
    public static void doStandardResound(){
        Map<String, Object> request = new HashMap<String, Object>();

        request.put("orgId",ORGID);
        request.put("orgMerCode",ORGMERCODE);
        request.put("orgTermNo",ORGTERMNO);
        request.put("orgTrace",ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        request.put("prodCode","ISYH");

        Map<String, Object> bizData = new HashMap<>();
        bizData.put("oriBizDate", "20211230");//原交易日期
        bizData.put("oriOrgTrace", "222222222226");//原机构交易流水号
        bizData.put("transAmt", "1");//退货金额
        request.put("bizData", bizData);
        String sign = sign(bizData, TEST_MER_RSA_PRIVATE_KEY);
        request.put("sign",sign);
        request.put("signType",SIGN_TYPE_RSA256);

        String requestStr = JSON.toJSONString(request);
        try {
            String responce = HttpConnectUtils.sendHttpSRequest(CASHIER_URL + "/cashier/trade/standardRefund", requestStr, "JSON", null);
            System.out.println("responce ===>"+responce);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     *  预下单接口-H5版 -- 交易预下单，唤起收银台/支付页面
     * 1、机构调用预下单接口生成预支付订单页面
     * 2、返回商户页面支付 redirectUrl 地址
     * 3、机构调用该地址完成下单支付
     *
     * 请求报文：{"bizData":{"appId":"","frontUrl":"","notifyUrl":"后台通知地址","openId":"-","subject":"订单描述","timeoutMinutes":"1800","title":"订单标题","transAmt":"1"},"orgId":"ORG000000000053","orgMerCode":"531000012972310","orgTermNo":"W1489510","orgTrace":"ORG0005320211023112307650809","prodCode":"ISYH","sign":"2/sGCBCGXN5ilqBQ341ihb+51MYZ9boxaiFch5FCCq3eF0od4FZLrwnSy/0FpyBaa2G4/3eLpT/W0RWFGDeNKdZsaiZ0Xx0OmFWUCEsjUQbgVdFTW7LnN9aRrPNmGSYXWS7Hel8618LPwvY0/xwPCLHWtdHC8qetEDcpuefr9MM=","signType":"RSA2"}
     * 返回报文： {"orgTrace":"ORG0005320211023112307650809","prodCode":"ISYH","sign":"VyItJodqYDbIPGBJT88yNJhp32j2ADchA8c2M6fNM/lVlLD32rS3mlf0X2qfio9UOAO6/8s++N75jbX+I4sQR4eeKOta01bcUeOPOoIHnPI55Vs4421Y7d2GZAzcyWQa4da136KaCv3Y8IUy7TJEKz7bvana6XTx1tNUbBiKiu4=","orgMerCode":"531000012972310","signType":"RSA2","sysRetCode":"000000","bizData":{"redirectUrl":"https://mtest.eycard.cn/cashier/index.html?merName=%E6%B9%96%E5%8D%97%E5%8A%A9%E8%AE%A2%E4%BF%A1%E6%81%AF%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&transAmt=1&functions=%5B%7B%22disCount%22%3A%22%E6%BB%A1200-199%22%2C%22funcId%22%3A%222%22%2C%22funcName%22%3A%22%E6%94%AF%E4%BB%98%E5%AE%9D%22%7D%2C%7B%22disCount%22%3A%22%E6%BB%A1200-201%22%2C%22funcId%22%3A%223%22%2C%22funcName%22%3A%22%E5%BE%AE%E4%BF%A1%22%7D%2C%7B%22funcId%22%3A%2212%22%2C%22funcName%22%3A%22%E4%BA%91%E9%97%AA%E4%BB%98%22%7D%5D&payToken=AVI0foGZIam0ZFM6OddN&payExpireTime=1634961191&openId=-","tradeRetMsg":"成功","tradeRetCode":"00"},"orgId":"ORG000000000053","orgTermNo":"W1489510"}
     */
    public static void doH5AdvancePay() {
        H5AdvancePay request = new H5AdvancePay();
        request.setSignType(SIGN_TYPE_RSA256);
        request.setOrgId(ORGID);
        request.setOrgMerCode(ORGMERCODE);
        request.setOrgTermNo(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        // 业务产品代码
        request.setProdCode("ISYH");
        request.setOrgDeviceIp("192.168.5.131");


        H5AdvancePay.BizDataBean dataBean = new H5AdvancePay.BizDataBean();
        dataBean.setTitle("订单标题");
        dataBean.setSubject("订单描述");

        // 支付宝、微信、云闪付用户授权唯一标识(如果没有，送:-)    H5 不能传-，  小程序可以传-
//        dataBean.setOpenId("oIcV7w-8iHNq8ERvqikH1YiBxQe4"); // oIcV7wwvKsNAxdnTNhg2ylI4Oibo
        // 支付宝、微信、云闪付商户应用ID（一般情况下与获取’openId’时的应用一致）
//        dataBean.setAppId("wxde94a63529b9b709");

    // openid = 微信号 + appid
//        dataBean.setOpenId("oVoLCwZzRVHwPrG3JvDs5Z_yc83s");  // 凯杰 微信
//        dataBean.setAppId("wxee597080118d5c2d");         // 凯杰 微信

        // 支付宝
        dataBean.setOpenId("2088402489184084");  // 凯杰 微信
        dataBean.setAppId("1266000048310000");         // 凯杰 微信

//        dataBean.setOpenId("oxme5wtl5jsLNN6mFT1B6DEmPZ_M");  // 凯杰 微信
//        dataBean.setAppId("wxee09d0d81067a28f");         // 凯杰 微信



        // 订单支付超时时间,不填默认1800（30分钟）
        dataBean.setTimeoutMinutes("1800");
        // 单位分
        dataBean.setTransAmt("1");
        // 留空则不通知
        dataBean.setNotifyUrl("https://notify-test.eycard.cn:7443/WorthTech_PayChannelModule_V1/Channel008/UnionPay_Weixin_Servlet");
        // 用户支付成功后，前端跳转地址。留空则支付后关闭webview
        dataBean.setFrontUrl("https://www.baidu.com");

        request.setBizData(dataBean);

        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest( CASHIER_URL + "/cashier/apply/advancePay", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkBizSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     *  预下单接口-小程序版 -- 【小程序】交易预下单，唤起收银台/支付页面，并唤起小程序
     * 1、机构调用小程序-预下单接口生成预支付订单页面
     * 2、返回商户页面支付 redirectUrl 地址
     * 3、机构调用该地址，唤醒小程序，完成下单支付
     *
     * 请求报文为：{"bizData":{"frontUrl":"","notifyUrl":"后台通知地址","openId":"-","subject":"订单描述","timeoutMinutes":"1800","title":"订单标题","transAmt":"1"},"orgId":"ORG000000000053","orgMerCode":"531000012972310","orgTermNo":"W1489510","orgTrace":"ORG0005320211023120315204148","prodCode":"ISYH","sign":"2/sGCBCGXN5ilqBQ341ihb+51MYZ9boxaiFch5FCCq3eF0od4FZLrwnSy/0FpyBaa2G4/3eLpT/W0RWFGDeNKdZsaiZ0Xx0OmFWUCEsjUQbgVdFTW7LnN9aRrPNmGSYXWS7Hel8618LPwvY0/xwPCLHWtdHC8qetEDcpuefr9MM=","signType":"RSA2"}
     * \n响应报文：{"orgTrace":"ORG0005320211023120315204148","prodCode":"ISYH","sign":"HqDagQF2owl2Ks5BYn1acfhCn05a7+HnOJL9snWYIqRjB0+06lOs1lNBKNEgbWaH6b2V0l4KuUUfXX+1YIhmjGBojlD8zu8uu8imL17KvG/ndOwTzrFsZF7dMKyZmDZdWOvY2p1fIPqS1aId6A73JiJ6FvVft1Ve7OZCtDIFFGU=","orgMerCode":"531000012972310","signType":"RSA2","sysRetCode":"000000","bizData":{"redirectUrl":"https://mtest.eycard.cn/cashier/index.html?params=%7B%22payExpireTime%22%3A1634963598%2C%22payToken%22%3A%2282DWpqQU1nrLnix6bf4U%22%7D","tradeRetMsg":"成功","tradeRetCode":"00"},"orgId":"ORG000000000053","orgTermNo":"W1489510"}
     * */
    public static void doSPPAdvancePay() {
        SAPPAdvancePay request = new SAPPAdvancePay();
        request.setSignType(SIGN_TYPE_RSA256);
        request.setOrgId(ORGID);
        request.setOrgMerCode(ORGMERCODE);
        request.setOrgTermNo(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        // 业务产品代码
        request.setProdCode("ISYH");
//        request.setProdCode("");
        request.setOrgDeviceIp("192.168.5.131");


        SAPPAdvancePay.BizDataBean dataBean = new SAPPAdvancePay.BizDataBean();
        dataBean.setTitle("订单标题");
        dataBean.setSubject("订单描述");

        // 支付宝、微信、云闪付用户授权唯一标识(如果没有，送:-)  H5 不能传-，  小程序可以传-
        dataBean.setOpenId("o8Zb55JvyvYyKponmJrXvUC-Tizg");
        // 支付宝、微信、云闪付商户应用ID（一般情况下与获取’openId’时的应用一致）
//        dataBean.setAppId("wx4629d39fddf7b65b");
        // 订单支付超时时间,不填默认1800（30分钟）
        dataBean.setTimeoutMinutes("1800");
        // 单位分
        dataBean.setTransAmt("1");
//        // 留空则不通知
//        dataBean.setNotifyUrl("https://wm.sxeldb.com/");
//        // 用户支付成功后，前端跳转地址。留空则支付后关闭webview
//        dataBean.setFrontUrl("https://wm.sxeldb.com/");

        request.setBizData(dataBean);

        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest( CASHIER_URL + "/cashier/sAppApply/advancePay", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkBizSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *场景说明
     * 1、与“小程序预下单-标准”的区别在于，本接口支持分账（接口字段无区别，地址不同）
     * 2、交易预下单，唤起收银台/支付页面
     *
     * 接口流程描述
     * 1、调用产品服务端预下单接口；传入商户订单信息、商户号、终端号、金额
     * 2、对商户进行验签、拆包；进行商户认证、路由判定、通过后生成主订单记录
     * 3、根据商户唯一信息生成TOKEN，重定向至JS页面，返回页面商户信息、金额、功能列表（支付宝、微信、云闪付）、时间
     */
    public static void doAdvancePaySpecial() {
        SAPPAdvancePay request = new SAPPAdvancePay();
        request.setSignType(SIGN_TYPE_RSA256);
        request.setOrgId(ORGID);
        request.setOrgMerCode(ORGMERCODE);
        request.setOrgTermNo(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        // 业务产品代码
        request.setProdCode("ISYH");
//        request.setProdCode("");
        request.setOrgDeviceIp("192.168.5.131");


        SAPPAdvancePay.BizDataBean dataBean = new SAPPAdvancePay.BizDataBean();
        dataBean.setTitle("订单标题");
        dataBean.setSubject("订单描述");

        // 支付宝、微信、云闪付用户授权唯一标识(如果没有，送:-)  H5 不能传-，  小程序可以传-
        dataBean.setOpenId("o8Zb55JvyvYyKponmJrXvUC-Tizg");
        // 支付宝、微信、云闪付商户应用ID（一般情况下与获取’openId’时的应用一致）
//        dataBean.setAppId("wx4629d39fddf7b65b");
        // 订单支付超时时间,不填默认1800（30分钟）
        dataBean.setTimeoutMinutes("1800");
        // 单位分
        dataBean.setTransAmt("1");
//        // 留空则不通知
//        dataBean.setNotifyUrl("https://wm.sxeldb.com/");
//        // 用户支付成功后，前端跳转地址。留空则支付后关闭webview
//        dataBean.setFrontUrl("https://wm.sxeldb.com/");

        request.setBizData(dataBean);

        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest( CASHIER_URL + "/cashier/sAppApply/advancePaySpecial", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkBizSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *  订单查询接口
     * 1、调用产品服务端订单查询接口；传入商户订单流水、交易日期
     * 2、返回订单信息
     *
     * 请求报文为：{"bizData":{"frontUrl":"","notifyUrl":"后台通知地址","openId":"-","subject":"订单描述","timeoutMinutes":"1800","title":"订单标题","transAmt":"1"},"orgId":"ORG000000000053","orgMerCode":"531000012972310","orgTermNo":"W1489510","orgTrace":"ORG0005320211023120315204148","prodCode":"ISYH","sign":"2/sGCBCGXN5ilqBQ341ihb+51MYZ9boxaiFch5FCCq3eF0od4FZLrwnSy/0FpyBaa2G4/3eLpT/W0RWFGDeNKdZsaiZ0Xx0OmFWUCEsjUQbgVdFTW7LnN9aRrPNmGSYXWS7Hel8618LPwvY0/xwPCLHWtdHC8qetEDcpuefr9MM=","signType":"RSA2"}
     * \n响应报文：{"orgTrace":"ORG0005320211023120315204148","prodCode":"ISYH","sign":"HqDagQF2owl2Ks5BYn1acfhCn05a7+HnOJL9snWYIqRjB0+06lOs1lNBKNEgbWaH6b2V0l4KuUUfXX+1YIhmjGBojlD8zu8uu8imL17KvG/ndOwTzrFsZF7dMKyZmDZdWOvY2p1fIPqS1aId6A73JiJ6FvVft1Ve7OZCtDIFFGU=","orgMerCode":"531000012972310","signType":"RSA2","sysRetCode":"000000","bizData":{"redirectUrl":"https://mtest.eycard.cn/cashier/index.html?params=%7B%22payExpireTime%22%3A1634963598%2C%22payToken%22%3A%2282DWpqQU1nrLnix6bf4U%22%7D","tradeRetMsg":"成功","tradeRetCode":"00"},"orgId":"ORG000000000053","orgTermNo":"W1489510"}
     * */
    public static void doTradeQuery() {
        TradeQuery request = new TradeQuery();
        request.setSignType(SIGN_TYPE_RSA256);
        request.setOrgId(ORGID);
        request.setOrgMerCode(ORGMERCODE);
        request.setOrgTermNo(ORGTERMNO);
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));
        // 业务产品代码
        request.setProdCode("ISYH");


        TradeQuery.BizDataBean dataBean = new TradeQuery.BizDataBean();
        dataBean.setOriBizDate("20220125");
        dataBean.setOriOrgTrace("ORG0000120220121154331680003");

        request.setBizData(dataBean);

        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest( CASHIER_URL + "/cashier/trade/query", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkBizSign(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @Description 用于后台交易通知, 此处仅仅是模拟返回报文， 实际用户在自己的系统定义接口接手
     *
     * 接口上送的参数(notifyUrl)，接收地址需公网可访问。
     * 最多通知5次，首次通知失败后，将最多进行4次补偿通知，
     * 补偿期间通知失败间隔补偿时间为：2s、8s、32s、128s，补偿期间通知成功和补偿次数结束将不再进行通知。
     * @date: 2021/10/23 13:41
     * @author: pandans
     */
    public String notify(Notice notice) {
        // 检验签名
        checkBizSign(JSON.toJSONString(notice));
        // 获取订单信息， 收到通知后，商户自行处理系统逻辑
        // 商户流水号 -- 一般用于客户更新订单状态
        String outTrace = notice.getBizData().getOutTrace();
        // 交易状态 00表示成功
        String tradeRetCode = notice.getBizData().getTradeRetCode();
        //        业务日期	tradeDate	C	成功时返回
        //        业务时间	tradeTime	C	成功时返回
        //        清算日期	stlmDate	C	成功时返回
        //        清算时间	stlmTime	C	成功时返回


        // 接收到请返回tradeRetCode，易生收到后认为通知成功，如果不反悔易生会按照补偿规则发送。
        return "{\"tradeRetCode\":\"000000\"}";
    }

    /**
     * @param qrAppUpIdentifier 云闪付执行qrAuth get方法获取的用户appupidentifier
     * @Description 云闪付获取用户标识
     * @date: 2021/10/14 23:20
     * @author: pandans
     */
    public static void doQrGetUserId(String qrAppUpIdentifier) {
        UnionUserId request = new UnionUserId();
        request.setOrgId(ORGID);
        request.setOrgMercode(ORGMERCODE);
        request.setOrgTermno(ORGTERMNO);
        request.setSignType(SIGN_TYPE_RSA256);
        // 交易流水，28位定长，规则：orgId 前四位+后四位+yyyyMMddhhmmss+6位自定义 合计28位长度（唯一）
        request.setOrgTrace(ORGID.substring(0, 4) + ORGID.substring(ORGID.length() - 4) + StringUtils.getLocalDate() + RandomUtil.generateNumString(6));

        UnionUserId.DataBean dataBean = new UnionUserId.DataBean();
        //授权码（付款方返回的临时授权码，一次有效）测试方法：微信支付宝，云闪付付款码然后点击上面轻触可以查看收款码数字
        dataBean.setQrUserAuthCode("6226176223328162414");
        dataBean.setQrAppUpIdentifier(qrAppUpIdentifier);

        request.setData(dataBean);
        // 使用商户私钥对data字段加签
        String sign = sign(dataBean, TEST_MER_RSA_PRIVATE_KEY);
        request.setSign(sign);

        String requestStr = JSON.toJSONString(request);

        try {
            String response = HttpConnectUtils.sendHttpSRequest(CASHIER_URL + "/cashier/authorize/qrUserId", requestStr, "JSON", null);
            System.out.println("\n响应报文：" + response);
            checkSign(response);

            // 云闪付用户id
            JSONObject rep = JSON.parseObject(response);
            if (RET_CODE_SUCCESS.equals(rep.getString("sysRetCode"))) {
                String payerId = rep.getJSONObject("bizData").getString("payerId");
                System.out.println("云闪付开放ID:" + payerId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 云闪付获取授权码  ！！是获取opneid的前置条件
     */
    public static String doYsfQrAuth() {
        // 1. 使用get方法获取云闪付的零时用户授权

        // 重定向地址（自己服务器地址，用于接收授权码）要提前在 银联加白名单 且只能加生产的。
        String redirectUrl = "https://www.baidu.com";
        String url = CASHIER_URL + "/cashier/authorize/qrAuth?qrRedirectUrl=" + "redirectUrl";

        // 2. 如上url在云闪付中执行， 云闪付会自动重定向到 redirectUrl，然后商户自行根据接口文档步骤获取到appupidentifier
        // 从HttpRequest获取参数，如用Springboot中， notify(HttpServletRequest request, HttpServletResponse response)
        // String userAgent = request.getHeader("user-agent");
        // 正式调用应该是再服务上获取 redirectUrl 对应的request， 此处直接模拟
        String userAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/ UnionPay/1.0 TESTPAY";
        String uaReg = ".*(UnionPay/[^ ]+ ([0-9a-zA-Z]+)).*";
        Pattern p = Pattern.compile(uaReg);
        Matcher m = p.matcher(userAgent);
        if (!m.matches()) {
            System.out.println("非银联云闪付APP。");
            return "";
        }
        String appUpIdentifier = m.group(1);
        // 3. appupidentifier 仅仅一次使用，过期请重复上面1，2。 该appupidentifier 用于获取云闪付的userid
        return appUpIdentifier;
    }


}
